""""
we don't need to "declare" spam_amount before assigning to it,
we don't need to tell Python what type of value spam_amount is going to refer to.
 In fact, we can even go on to reassign spam_amount to refer to a different sort of thing like a string or a boolean.
"""
spam_amount = 0
print(spam_amount)
print(type(spam_amount))

apple_kilo = 2.45
print(type(apple_kilo))

# Ordering Spam, egg, Spam, Spam, bacon and Spam (4 more servings of Spam)
spam_amount = spam_amount + 4

if spam_amount > 0:
    print("But I don't want ANY spam!")

viking_song = "Spam " * spam_amount
print(viking_song)

print("========operators in python=======")
# operators in python
print(5 / 2)
print(5 // 2)
print(4 / 2)  # 强制存为float
print(4 // 2)  # 强制存为int

print(min(1, -2, 3, 4))
print(max(1.0, 3.3, 4.45))
print(abs(-23))
print(abs(23))

# In addition to being the names of Python's two main numerical types,
# int and float can also be called as functions
# which convert their arguments to the corresponding type
print(float(10))
print(int(3.33))
# They can even be called on strings!
print(int('807') + 1)

"""
python函数
"""
print("========function in python=======")

help(round)

help(round(-2.01))

"""
Good programmers use docstrings 
unless they expect to throw away the code soon after it's used (which is rare). 
So, you should start writing docstrings, too!
"""


def least_difference(a, b, c):
    """Return the smallest difference between any two numbers
    among a, b and c.

    # >>> least_difference(1, 5, -5)
    # None
    """
    diff1 = abs(a - b)
    diff2 = abs(b - c)
    diff3 = abs(a - c)
    # 也可以没有返回值，python会将返回值改为none
    # return min(diff1, diff2, diff3)


help(least_difference)

"""
Default arguments in function
"""
print("sd", 1.34, min(2, 3, 4), sep="$")
print("sd", 1.34, min(2, 3, 4))


def greet(who="Colin"):
    """
    test function for default arguments
    :param who: default is "Colin"
    :return:
    """
    print("Hello,", who)


greet()
greet(who="Kaggle")
# (In this case, we don't need to specify the name of the argument, because it's unambiguous.)
greet("world")

"""
Functions that operate on other functions are called "higher-order functions." 
You probably won't write your own for a little while. 
But there are higher-order functions built into Python that you might find useful to call.

Here's an interesting example using the max function.

By default, max returns the largest of its arguments. 
But if we pass in a function using the optional key argument, 
it returns the argument x that maximizes key(x) (aka the 'argmax').
"""


def mod_5(x):
    """Return the remainder of x after dividing by 5"""
    return x % 5


print(
    [100,51,14],
    'Which number is biggest?',
    max(100, 51, 14),
    'Which number is the biggest modulo 5?',
    max(100, 51, 14, key=mod_5),
    sep='\n',
)
